package com.cskaoyan.service;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.WxSearchHelperVo;
import com.cskaoyan.bean.vo.WxSearchIndexVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Trellis
 * @since 2022/09/08 21:24
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;


    @Override
    public BaseRespVo selectAll(Integer userId) {
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();
        MarketKeywordExample example = new MarketKeywordExample();
        //example.createCriteria().andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        for (MarketKeyword marketKeyword : marketKeywords) {
            wxSearchIndexVo.CopyHotKeywordList(marketKeyword);
        }
        Integer id = marketKeywords.get(0).getId();
        MarketKeyword marketKeyword = marketKeywordMapper.selectByPrimaryKey(id);
        wxSearchIndexVo.CopyDefaultKeyword(marketKeyword);
        //这里是直接查询历史记录，后序需要根据use的id进行查询
        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        historyExample.createCriteria().andDeletedEqualTo(false);
        //这是后续获取user的id的代码
        MarketSearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
            List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(historyExample);
            for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
                wxSearchIndexVo.CopyHistoryKeywordList(marketSearchHistory);
            }
            return BaseRespVo.ok(wxSearchIndexVo);
        }

        return BaseRespVo.invalidData();
    }

    @Override
    public WxSearchHelperVo selectByKeyword(String keyword,Integer id) {
        //MarketKeywordExample example = new MarketKeywordExample();
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        if (keyword != null) {
            criteria.andNameLike("%"+keyword+"%");
        }
        //List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        WxSearchHelperVo wxSearchHelperVo = new WxSearchHelperVo();
        for (MarketGoods marketKeyword : marketGoods) {
            wxSearchHelperVo.copyData(marketKeyword);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String format = dateFormat.format(date);
        //获取数据库中类型的时间
        Date time = null;
        try {
            time = dateFormat.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setUserId(id);
        marketSearchHistory.setKeyword(keyword);
        marketSearchHistory.setAddTime(time);
        marketSearchHistory.setUpdateTime(time);
        marketSearchHistory.setDeleted(false);

        marketSearchHistoryMapper.insertSelective(marketSearchHistory);

        wxSearchHelperVo.setErrno(0);
        wxSearchHelperVo.setErrmsg("成功");
        return wxSearchHelperVo;
    }

    @Override
    public BaseRespVo clearhistory(Integer id) {
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        //临时测试，最后要根据user的id进行删除
        //int delete = marketSearchHistoryMapper.deleteByExample(example);
        example.createCriteria().andUserIdEqualTo(id);
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setDeleted(true);
        marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory,example);

        return BaseRespVo.ok();
    }
}
